$(function(){
	var basePath=$("#basepath").val();
	//筛选->读取系
	$("#branchFilter_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#branchFilter_form select[name='department']");
		$depSelect.html("");
		var $proSelect=$("#branchFilter_form select[name='profession']");
		$proSelect.html("");
		$(" <option value='0'>不限</option>").appendTo($proSelect);
		if(collegeId==0){
			$(" <option value='0'>不限</option>").appendTo($depSelect);
			return;
		}
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						$(" <option value='0'>不限</option>").appendTo($depSelect);
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
					}
			}, "json");
	});
	//筛选->读取专业
	$("#branchFilter_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $proSelect=$("#branchFilter_form select[name='profession']");
		$proSelect.html("");
		if(departmentId==0){
			$(" <option value='0'>不限</option>").appendTo($proSelect);
			return;
		}
		$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						$(" <option value='0'>不限</option>").appendTo($proSelect);
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($proSelect);
						}
					}
			}, "json");
	});
	//筛选按钮
	$("#branchFilter_go").click(function(){
		var clgId=$("#branchFilter_form select[name='college']").val();
		var depId=$("#branchFilter_form select[name='department']").val();
		var proId=$("#branchFilter_form select[name='profession']").val();
		window.location=basePath+"admin/branch/showBranchList/"+clgId+ "/"+depId+"/"+proId+"/1";
	});
	//编辑按钮
	$(".branch_edit").click(function(){
		var branch_id=$(this).attr("data-branchId");
		$("#branch_edit_box_alert_container").html("");//清空提示框
		$.get(basePath+ "admin/branch/getProBranch/"+branch_id,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						var collegeId=data.prof.department.college.id;
						var departmentId=data.prof.department.id;
						var professionId=data.prof.id;
						$("#branch_edit_box_form input[name='branch_no']").val(data.no);
						$("#branch_edit_box_form input[name='branch_name']").val(data.name);
						$("#branch_edit_box_form input[name='branch_id']").val(data.id);
						$("#branch_edit_box_form select[name='college']").val(collegeId);
						//生成系选择框
						var $depSelect=$("#branch_edit_box_form select[name='department']");
						$depSelect.html("");
						$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
								function(data_dep,textStatus_dep){
									if(textStatus_dep=="success"){
										if(data_dep.error>0){
											alert("登陆状态已过期,请重新登陆");
											window.location=basePath;
											return;
										}
										for(i=0;i<data_dep.length;i++){
											 $(" <option/>").val(data_dep[i].id).html(data_dep[i].name).appendTo($depSelect);
										}
										$depSelect.val(departmentId);
										//生成专业选择框
										var $proSelect=$("#branch_edit_box_form select[name='profession']");
										$proSelect.html("");
										$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
												function(data_pro,textStatus_pro){
													if(textStatus_pro=="success"){
														if(data_pro.error>0){
															alert("登陆状态已过期,请重新登陆");
															window.location=basePath;
															return;
														}
														for(i=0;i<data_pro.length;i++){
															$(" <option/>").val(data_pro[i].id).html(data_pro[i].name).appendTo($proSelect);
														}
														$proSelect.val(professionId);
														$("#branch_edit_box .ajax_wait").hide();
														$("#branch_edit_box_form").show();
													}
											}, "json");
									}
							}, "json");
					}
			}, "json");
	});
	$("#branch_edit_box_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#branch_edit_box_form select[name='department']");
		$depSelect.html("");
		var $proSelect=$("#branch_edit_box_form select[name='profession']");
		$proSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("#branch_edit_box_alert_container"),"no_dep_error");
						}else{
							$("#branch_edit_box_alert_container .no_dep_error").alert('close');
						}
						$depSelect.change();
					}
			}, "json");
	});
	
	$("#branch_edit_box_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $proSelect=$("#branch_edit_box_form select[name='profession']");
		$proSelect.html("");
		$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($proSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($proSelect);
							makeAlert("无可用专业,请添加之后再试","error",$("#branch_edit_box_alert_container"),"no_pro_error");
						}else{
							$("#branch_edit_box_alert_container .no_pro_error").alert('close');
						}
					}
			}, "json");
	});

	//修改保存
	$("#branch_edit_box_save").click(function(){
		var departmentId=$("#branch_edit_box_form select[name='department']").val();
		var professionId =$("#branch_edit_box_form select[name='profession']").val();
		var branchId =$("#branch_edit_box_form input[name='branch_id']").val();
		var branchNo =$("#branch_edit_box_form input[name='branch_no']").val();
		var branchName =$("#branch_edit_box_form input[name='branch_name']").val();
		if(professionId==0){
			makeAlert("无可用专业,请添加之后再试","error",$("#branch_edit_box_alert_container"),"no_pro_error");
			return;
		}
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#branch_edit_box_alert_container"),"no_dep_error");
			return;
		}
		$("#branch_edit_box_alert_container .empty_field_error").alert('close');
		$("#branch_edit_box_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/branch/updateProBranch",{id:branchId,no:branchNo,name:branchName,profId:professionId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-3":
							makeAlert("字段不允许为空,请重新检查","error",$("#branch_edit_box_alert_container"),"empty_field_error");
							break;
						case "-4":
							makeAlert("已存在编号为"+branchNo+"的专业方向,请检查后再输入","error",$("#branch_edit_box_alert_container"),"repeat_no_error");
							break;
						case "-5":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							$("#branch_edit_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$(".branch_delete").click(function(){
		$("#branch_delete_box input[name='branchId']").val($(this).attr("data-branchId"));
	});
	$("#branch_delete_box_delete").click(function(){
		var branchId=$("#branch_delete_box input[name='branchId']").val();
		$.get(basePath+ "admin/branch/deleteBranch/"+branchId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							noty({text:"删除错误:此专业方向之下还有未删除的班级",type:"error"});
							$("#branch_delete_box").modal('hide');
							break;
						case "-2":
							noty({text:"删除错误:此专业方向之下还有未删除的题目",type:"error"});
							$("#branch_delete_box").modal('hide');
							break;
						case "-3":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#branch_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//增加专业方向
	$("#branch_add").click(function(){
		$("branch_add_box_alert_container").html("");
		$("#branch_add_box_form select[name='college']").change();
	});
	$("#branch_add_box_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#branch_add_box_form select[name='department']");
		$depSelect.html("");
		var $proSelect=$("#branch_add_box_form select[name='profession']");
		$proSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							 $(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							makeAlert("无可用系,请添加之后再试","error",$("branch_add_box_alert_container"),"no_dep_error");
						}else{
							$("#branch_add_box_alert_container .no_dep_error").alert('close');
						}
						$depSelect.change();
					}
			}, "json");
	});
	$("#branch_add_box_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $proSelect=$("#branch_add_box_form select[name='profession']");
		$proSelect.html("");
		$.get(basePath+ "admin/branch/getProsOfDeps/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($proSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($proSelect);
							makeAlert("无可用专业,请添加之后再试","error",$("#branch_add_box_alert_container"),"no_pro_error");
						}else{
							$("#branch_add_box_alert_container .no_pro_error").alert('close');
						}
					}
			}, "json");
	});
	$("#branch_add_box_save").click(function(){
		var departmentId=$("#branch_add_box_form select[name='department']").val();
		var professionId =$("#branch_add_box_form select[name='profession']").val();
		var branchNo =$("#branch_add_box_form input[name='branch_no']").val();
		var branchName =$("#branch_add_box_form input[name='branch_name']").val();
		if(professionId==0){
			makeAlert("无可用专业,请添加之后再试","error",$("#branch_add_box_alert_container"),"no_pro_error");
			return;
		}
		if(departmentId==0){
			makeAlert("无可用系,请添加之后再试","error",$("#branch_add_box_alert_container"),"no_dep_error");
			return;
		}
		$("#branch_add_box_alert_container .empty_field_error").alert('close');
		$("#branch_add_box_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/branch/insertProBranch/",{no:branchNo,name:branchName,profId:professionId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#branch_add_box_alert_container"),"empty_field_error");
							break;
						case "-3":
							makeAlert("已存在编号为"+branchNo+"的专业方向,请检查后再输入","error",$("#branch_add_box_alert_container"),"repeat_no_error");
							break;
						case "-4":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							$("#branch_add_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});